package com.kim.user.web.controller;

import com.kim.common.resp.CodeMsg;
import com.kim.common.resp.Result;
import com.kim.user.common.entity.SysRole;
import com.kim.user.web.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Mr.Yangxiufeng
 * Date: 2018-06-13
 * Time: 10:27
 */
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("getRoleByUserId/{userId}")
    public Result getRoleByUserId(@PathVariable("userId") Integer userId) {
        List<SysRole> roleList = roleService.getRoleByUserId(userId);
        return Result.success(roleList);
    }

    @GetMapping("/insert")
    public Result insert(@RequestParam String roleName) {
//        log.info("find rolename : " + roleName);
        SysRole byRolename = roleService.findByRolename(roleName);
        if (byRolename != null) {
            log.info("role 校验不通过");
            return Result.error(CodeMsg.ROLE_NAME_EXIST);
        }
        log.info("insert rolename : " + roleName);
        SysRole sysRole = new SysRole();
        sysRole.setName(roleName);
        sysRole.setStatus(1);
        int i = roleService.insertRole(sysRole);
        if (i != 1) {
            return Result.error(CodeMsg.SAVE_FAIL);
        } else {
            return Result.success("保存成功");
        }
    }
}
